$(function() {
	// 初始化选中的标签
	var tab = $('.suredy-tabs>ul');
	if (tab.data('active-tab')) {
		$(tab.data('active-tab') + '>a').tab('show');
	}

	// 刷新页面
	$.suredy.refreshContent = function(contractId, pid, selector/* css选择器，可以为选择其的数组 */) {
		if (!contractId)
			return;

		var sls = [];

		if ($.isArray(selector)) {
			sls = selector;
		} else if (typeof selector === 'string') {
			sls[sls.length] = selector;
		}

		// 增加操作按钮的刷新
		sls[sls.length] = '#op-btns';
		// 增加操作日志的刷新
		sls[sls.length] = '.op-logs';

		var activeTabId = $('.suredy-tabs ul.nav-tabs>li.active').attr('id');

		var url = $.suredy.contextPath + '/sub-contract/view/' + contractId;

		$.get(url, function(html, textStatus, jqXHR) {
			var $html = $(html);

			$.each(sls, function(i, val) {
				$(val).html($(val, $html).html());

				$('#' + activeTabId + '>a').tab('show');
			});
		}, 'html');
	};

	// 移除文件
	$('body').on('click', 'a.remove-contract-file', function(event) {
		var $this = $(this);
		var id = $this.data('id');
		var cid = $this.data('cid');
		var pid = $this.data('pid');

		if (!id)
			alert('无效文件信息，无法删除！');

		$.ajax({
			url : $.suredy.contextPath + '/contract-file/del/' + id,
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '删除文件失败！');
					return false;
				}

				$this.parents('.file-item').remove();
				$.suredy.refreshContent(cid, pid);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('删除文件失败！\n\n' + errorThrown);
			}
		});
	});

	// 付款
	$('body').on('click', '.sub-pay-out', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-signout',
			title : '付款',
			showFoot : false,
			uri : $.suredy.contextPath + '/sub-contract/pay-out/' + $this.data('id') + '/' + $this.data('pid')
		});
	});

	// 上传合同
	$('body').on('click', '.upload-file', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-upload',
			title : '上传合同文件',
			showFoot : false,
			uri : $.suredy.contextPath + '/sub-contract/upload-file/' + $this.data('id') + '/' + $this.data('pid')
		});
	});

	// 完工结算
	$('body').on('click', '.clearing-contract', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-credit-card',
			title : '完工结算',
			showFoot : false,
			uri : $.suredy.contextPath + '/sub-contract/clearing-contract/' + $this.data('id') + '/' + $this.data('pid')
		});
	});

	// 付款计划
	$('body').on('click', '.sub-pay-out-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			size : 'lg',
			icon : 'icon-money',
			title : '付款计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/sub-contract/payout-plan-multiple/' + $this.data('id') + '/' + $this.data('pid')
		});
	});

	// 删除付款计划
	$('body').on('click', '.delete-payout-plan', function() {
		var $this = $(this);
		var id = $this.data('id');
		var cid = $this.data('cid');

		$.ajax({
			url : $.suredy.contextPath + '/sub-contract/delete-payout-plan/' + id,
			type : 'post',
			dataType : 'json',
			success : function(result, textStatus, jqXHR) {
				if (!result || !result.success) {
					alert(result.msg || '操作失败！');
					return false;
				}

				$.suredy.refreshContent(cid, undefined, [ 'div#tab3' ]);
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('操作失败！\n\n' + errorThrown);
			}
		});
	});

	// 编辑付款计划
	$('body').on('click', '.edit-payout-plan', function() {
		var $this = $(this);

		$.suredy.showModal({
			icon : 'icon-money',
			title : '变更付款计划',
			showFoot : false,
			uri : $.suredy.contextPath + '/sub-contract/payout-plan-change/' + $this.data('id')
		});
	});

	// 编辑表单
	$('body').on('click', '.edit-form', function() {
		var $this = $(this);

		location.href = $.suredy.contextPath + "/sub-contract/edit/?id=" + $this.data('id');
	});

});